import { Message } from 'element-ui'
const DEFAULT_DURATION = 2000
export default {
	install (Vue) {
		const $Message = (options) => {
			return new Promise(resolve => {
				const currentOptions = typeof options === 'string' ? { message: options } : options
				currentOptions.duration = currentOptions.duration || DEFAULT_DURATION
				Message(currentOptions)
				window.setTimeout(() => {
					resolve()
				}, 100)
			})
		}
		$Message.success = (message) => {
			return new Promise(resolve => {
				Message.success({
					message,
					duration: DEFAULT_DURATION
				})
				window.setTimeout(() => {
					resolve()
				}, 100)
			})
		}
		$Message.warning = (message) => {
			return new Promise(resolve => {
				Message.warning({
					message,
					duration: DEFAULT_DURATION
				})
				window.setTimeout(() => {
					resolve()
				}, 100)
			})
		}
		$Message.error = (message) => {
			return new Promise(resolve => {
				Message.error({
					message,
					duration: DEFAULT_DURATION
				})
				window.setTimeout(() => {
					resolve()
				}, 100)
			})
		}
		Vue.prototype.$fxMessage = $Message
	}
}

